<?php
/**
 * @version		$Id: manager.php 10381 2008-06-01 03:35:53Z yopensource $
 * @package		YOS News Crawler
 * @subpackage	Link Manager
 * @license		commercial
 * @author 		yopensource
 * @email		yopensource@gmail.com
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'phpWebHacks.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'find_replace.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'resource_url.php');
require_once(JPATH_COMPONENT_SITE.DS.'helpers'.DS.'autocuttext.php');

jimport('joomla.application.component.model');
// Set the table directory
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');

/**
 * Weblinks Component Weblink Model
 *
 * @package		Joomla
 * @subpackage	Content
 * @since 1.5
 */
class YOS_NewsModelLinkManager extends JModel
{
	/**
	 * amMap data array
	 *
	 * @var array
	 */
	var $_data = null;	

	/**
	 * uri total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;
	
	var $_linkEdit=null;
	
	var $errRemove=null;
	/**
	 * Constructor
	 *
	 * @since 0.9
	 */
	
	function __construct()
	{
		parent::__construct();

		global $mainframe,$option;
		
		//$context			= 'com_content.viewcontent';
		$filter_order		= $mainframe->getUserStateFromRequest( $option.'.linkmanager_newscrawler.filter_order', 		'filter_order', 	'id',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $option.'.linkmanager_newscrawler.filter_order_Dir',	'filter_order_Dir',	'',				'word' );		
		$search 			= $mainframe->getUserStateFromRequest( $option.'.linkmanager_newscrawler.search', 			'search', 			'',				'string' );
		$search 			= JString::strtolower( $search );		
		
		$limit				= $mainframe->getUserStateFromRequest($option.'.linkmanager_newscrawler.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart			= $mainframe->getUserStateFromRequest($option.'.linkmanager_newscrawler.limitstart', 'limitstart', 0, 'int');
		
		$this->setState('limit', 		$limit);
		$this->setState('limitstart', 	$limitstart);
		$this->setState('search', 		$search);
		// Get the filter request variables
		$this->setState('filter_order', 	$filter_order);
		$this->setState('filter_order_dir', $filter_order_Dir);
	}
	
	function getTotal()
	{
		// Lets load the total nr if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery(); 
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}
	
	function getPagination()
	{		
		// Lets load the content if it doesn't already exist
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}
	
		return $this->_pagination;
	}
	
	
	function getData(){
		$pop	= JRequest::getBool('pop');
		// Lets load the content if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();

			if ($pop) {
				$this->_data = $this->_getList( $query );
			} else {
				$this->_data = $this->_getList( $query, $this->getState('limitstart'), $this->getState('limit') );
			}
		}
	
		return $this->_data;
	}
	
	function _buildQuery(){
		// Get the WHERE and ORDER BY clauses for the query
		$where		= $this->_buildWhere();
		$orderby	= $this->_buildOrderBy();
		
		$config 	= &JComponentHelper::getParams('com_yos_news_crawler');
		$data_type 	= $config->get('data_type');	
		
			//Get Events from Database
		$query = ' SELECT ync.*
					FROM #__yos_news_crawler AS ync '.	$where. $orderby;
		return $query;
	}
	
	function _buildWhere( )
	{		
		
		$db					= &JFactory::getDBO();		
		$search				= $this->getState('search');	
		$where 				= array();
		
		if ( $search ) {
			$where[] = 'ync.title LIKE '.$db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
		}	
			
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
					
		return $where;
	}
	
	function _buildOrderBy()
	{
		$filter_order		=	$this->getState('filter_order');
		$filter_order_Dir	=	$this->getState('filter_order_dir');
		
		if ($filter_order == 'id'){
			$orderby 	= ' ORDER BY id';
		} else {
			$orderby 	= ' ORDER BY '. $filter_order .' '. $filter_order_Dir .', id';
		}
		
		return $orderby;
	}
	
	function getLists(){
		
		// state filter
		$lists['state'] = JHTML::_('grid.state', $this->getState('filter_state'), 'Published', 'Unpublished');
		
		// table ordering
		$lists['order_Dir']	= $this->getState('filter_order_dir');
		$lists['order']		= $this->getState('filter_order');
		
		// search filter
		$lists['search']= $this->getState('search');
		
		$filter		= null;
		$filter_sectionid		=	$this->getState('sectionid');
		$catid					=	$this->getState('catid');
		
		$config 	= &JComponentHelper::getParams('com_yos_news_crawler');
		$data_type 	= $config->get('data_type');	
	
		return $lists;
	}
	
	function filterCategory($query, $active = NULL)
	{
		// Initialize variables
		$db	= & JFactory::getDBO();

		$categories[] = JHTML::_('select.option', '0', '- '.JText::_('Select Category').' -');
		$db->setQuery($query);
		$categories = array_merge($categories, $db->loadObjectList());

		$category = JHTML::_('select.genericlist',  $categories, 'catid', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', $active);

		return $category;
	}
	
/* FOR TEST	*/
	function get_document_type()
	{
		$site_url = JRequest::getVar('url');
		$site_url = base64_decode($site_url);
		$browser = new phpWebHacks();
		$header = @$browser->head($site_url);
		
//		if(!$header || $header['Status']['Code'] != '200'){
		if(!isset($header['Content-Type'])){
			return '{"type":"false"}';			
		}
		if (preg_match('/xml/i', $header['Content-Type'])) {
			return '{"type":"xml"}';	
		}
		return '{"type":"http"}';
	}
	function step2_test()
	{
		$browser = new phpWebHacks();
		
		$json = JRequest::getString('json', null, null, 2);
		if (!$json)	return '{"result":"false", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (!$obj_json->link)	return '{"result":"false", "err_msg":"invalid url #2"}';
		
		$arr_links = array();
		
		if ($obj_json->document_type == 'xml') {			
			$options['rssUrl']		= $obj_json->link;
			$options['cache_time']	= 60;
			$rssDoc =& JFactory::getXMLparser('RSS', $options);
			
			if (!$rssDoc)	return	'{"result":"false", "err_msg":"unable to read xml file"}';			
			
			$items	=	$rssDoc->get_items();
			$items	=	array_slice($items , 0, 10);

			foreach ($items as $item) {
				$arr_links[] = $item->get_link();
			}	
		}
		else {
			//http
			$response = @$browser->get($obj_json->link);

			if (!$response) {
				return '{"result":"false", "err_msg":"unable to connect"}';				
			}
			$obj_json->link = $browser->get_addressbar();
			if (!preg_match_all($obj_json->getlink_reg, $response, $matches)) {
				return '{"result":"false", "err_msg":"Invalid regex"}';				
			}
			$arr_links = $matches[$obj_json->getlink_sub];
		}
		
		for ($i = 0; $i < count($arr_links); $i++){
			$resource_url = new YOS_Resource_Url($arr_links[$i], $obj_json->link);
			//Process link urls (href)
			$arr_links[$i] = $resource_url->process_single_url();
		}
				
		//find and replace here
		if($obj_json->find_replace_links){
			for ($i = 0; $i < count($arr_links);$i++){
				$arr_links[$i] = YOS_Find_Replace::find_replace($arr_links[$i], explode('|YOS_SEPARATOR|', $obj_json->find_replace_links));
			}
		}		
		$str_links = implode('|', $arr_links);
		
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->links = $str_links;
		$str_return = json_encode($obj_return);
		return $str_return;
	}
	
	function step2_test_link()
	{
		$browser = new phpWebHacks();
		
		$json = JRequest::getString('json', null, null, 2);
		if (!$json)	return '{"result":"false", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (!$obj_json->link)	return '{"result":"false", "err_msg":"invalid url #2"}';
		if (!isset($obj_json->index))	return '{"result":"false", "err_msg":"invalid index"}';
				
		if (!$browser->head($obj_json->link)) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"fail to connect"}';
		}
		
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->index = $obj_json->index;
		$obj_return->link = $browser->get_addressbar();
		$str_return = json_encode($obj_return);
		return $str_return;
	}
	function step3_test()
	{
		$yos_utility = new YOS_utility();
				
		$json = JRequest::getString('json', null, null, 2);
		if (!$json) 	return '{"result":"false", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (!$obj_json->link)	return '{"result":"false", "err_msg":"invalid url #2"}';		
		
		$arr_titles = array();
		$charset = false;
		if ($obj_json->document_type == 'xml') {
			//xml
			$options['rssUrl']		= $obj_json->link;
			$options['cache_time']	= 60;
			$rssDoc =& JFactory::getXMLparser('RSS', $options);
			
			if (!$rssDoc) return '{"result":"false", "err_msg":"unable to read xml file"}';			
			
			$items	=	$rssDoc->get_items();
			$items	=	array_slice($items , 0, 10);

			foreach ($items as $item) {
				$arr_titles[] = $item->get_title();
			}			
		}
		else {
			//http
			$browser = new phpWebHacks();
			$response = @$browser->get($obj_json->link);
						
			if (!$response)	return '{"result":"false", "err_msg":"unable to connect"}';	
			
			$charset = $browser->get_charset();
				
			if (!preg_match_all($obj_json->gettitle_reg, $response, $matches))	return '{"result":"false", "err_msg":"Invalid regex"}';			
			
			$arr_titles = $matches[$obj_json->gettitle_sub];
		}
				
		for ($i = 0; $i < count($arr_titles); $i++){
			$arr_titles[$i] = $yos_utility->charset_process($arr_titles[$i], $charset);
		}
						
		//Find and replace here
		if($obj_json->find_replace_titles){
			for ($i = 0; $i < count($arr_titles);$i++){
				$arr_titles[$i] = YOS_Find_Replace::find_replace($arr_titles[$i], explode('|YOS_SEPARATOR|', $obj_json->find_replace_titles));
			}			
		}
				
		$str_titles = implode('|', $arr_titles);
		
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->titles = $str_titles;
		$str_return = json_encode($obj_return);
		return $str_return;
	}
	function step5_test()
	{
		$yos_utility = new YOS_utility();
		
		$json = JRequest::getString('json', null, null, 2);
		if (!$json)	return '{"result":"false", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (@!$obj_json->link)	return '{"result":"false", "err_msg":"invalid url #2"}';
		
		if (!$obj_json->links)	return '{"result":"false", "err_msg":"invalid urls"}';
		
		$arr_links = explode('|', $obj_json->links);
		
		$arr_intros = array();
		$charset = false;
			
		if($obj_json->dataway == 0 || $obj_json->dataway == 3){
			$str_real_url = $obj_json->link;
			
			//connect to the link page then get intro
			if ($obj_json->document_type == 'xml') {
				//xml
				$options['rssUrl']		= $obj_json->link;
				$options['cache_time']	= 60;
				$rssDoc =& JFactory::getXMLparser('RSS', $options);
				
				if (!$rssDoc)	return '{"result":"false", "err_msg":"unable to read xml file"}';			
				
				$items	=	$rssDoc->get_items();
				$items	=	array_slice($items , 0, count($arr_links));
				
				foreach ($items as $item) {
					$arr_intros[] = $item->get_description();
				}
			}
			else {
				//http
				$browser = new phpWebHacks();
				$response = @$browser->get($obj_json->link);
				if (!$response)	return '{"result":"false", "err_msg":"unable to connect"}';
				
				$charset = $browser->get_charset();
				
				$str_real_url = $browser->get_addressbar();
				
				if (!preg_match_all($obj_json->getintro_reg, $response, $matches))	return '{"result":"false", "err_msg":"invalid regex"}';				
				
				$arr_intros = array_slice($matches[$obj_json->getintro_sub], 0, count($arr_links));
			}
			
			for ($i = 0; $i < count($arr_intros); $i++){
				$arr_intros[$i] = $yos_utility->charset_process($arr_intros[$i], $charset);
				
				$resource_url = new YOS_Resource_Url($arr_intros[$i], $str_real_url);
				//TODO: Process link urls (href)
				$arr_intros[$i] = $resource_url->process_href();
				
				//TODO: Process image source urls (src)
				$arr_intros[$i] = $resource_url->process_src();
			}		
		}
		else {
			return '{"result":"false", "err_msg":"invalid dataway"}';
		}
		
		//Find and replace here 
		if($obj_json->find_replace_intro){
			for ($i = 0; $i < count($arr_intros);$i++){
				$arr_intros[$i] = YOS_Find_Replace::find_replace($arr_intros[$i], explode('|YOS_SEPARATOR|', $obj_json->find_replace_intro));
			}
		}
		
		if(($obj_json->dataway == 2 || $obj_json->dataway == 3) && $obj_json->cut_content && $obj_json->numberword > 0){
			//TODO: Cut text here
			for ($i = 0; $i < count($arr_intros); $i++){
//				$auto_cutter = new AutoCutText($arr_intros[$i], $obj_json->numberword);
				$auto_cutter = new AutoCutText($arr_intros[$i], $obj_json->numberword);
				$arr_intros[$i] = $auto_cutter->getIntro(). '<hr />The text below is the fulltext data<hr />' .$auto_cutter->getFulltext();
			}
		}
		
		$str_intros = implode('|YOS_SEPARATOR|', $arr_intros);
		
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->intros = $str_intros;
		$str_return = json_encode($obj_return);
		return $str_return;		
	}
	
	function step5_test_a_link()
	{
		$yos_utility = new YOS_utility();
		
		$json = JRequest::getString('json', null, null, 2);
		
		if (!$json)	return '{"result":"false", "index":"NaN", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (@!$obj_json->link)	return '{"result":"false", "index":"NaN", "err_msg":"invalid url #2"}';
		
		if (!isset($obj_json->index))	return '{"result":"false", "index":"NaN", "err_msg":"invalid index"}';

		$intro = '';	
		
		$browser = new phpWebHacks();
		$response = @$browser->get($obj_json->link);
		
		if (!$response) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"unable to connect"}';
		}
		$link = $browser->get_addressbar();
		$charset = $browser->get_charset();
	
		if (!preg_match($obj_json->getintro_reg, $response, $matches)) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid intro regex"}';
		} 

		$intro = $matches[$obj_json->getintro_sub];
		
		$intro = $yos_utility->charset_process($intro, $charset);
		
		$resource_url = new YOS_Resource_Url($intro, $link);
		//TODO: Process link urls (href)
		$intro = $resource_url->process_href();
		
		//TODO: Process image source urls (src)
		$intro = $resource_url->process_src();
				
		//Find and replace here 
		if($obj_json->find_replace_intro){
			$intro = YOS_Find_Replace::find_replace($intro, explode('|YOS_SEPARATOR|', $obj_json->find_replace_intro));
		}
		
		if(($obj_json->dataway == 2 || $obj_json->dataway == 3) && $obj_json->cut_content && $obj_json->numberword > 0){
			//TODO: Cut text here
			$auto_cutter = new AutoCutText($intro, $obj_json->numberword);
			$intro = $auto_cutter->getIntro(). '<hr />The text below is the fulltext data<hr />' .$auto_cutter->getFulltext();
		}
						
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->index = $obj_json->index;
		$obj_return->intro = $intro;
		$str_return = json_encode($obj_return);
		return $str_return;		
	}
	
	function step6_test()
	{
		$yos_utility = new YOS_utility();
		
		$json = JRequest::getString('json', null, null, 2);
		if (!$json)	return '{"result":"false", "index":"NaN", "err_msg":"invalid url"}';

		$obj_json = json_decode($json);

		if (@!$obj_json->link)	return '{"result":"false", "index":"NaN", "err_msg":"invalid url #2"}';
		
		if (!isset($obj_json->index))	return '{"result":"false", "index":"NaN", "err_msg":"invalid index"}';
				
		$str_full = '';
			
		if($obj_json->dataway == 2 || $obj_json->dataway == 3){
			//do step5 but get cutted text
		}
						
		
		$browser = new phpWebHacks();
		$response = @$browser->get($obj_json->link);
		
		if (!$response) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"unable to connect"}';
		}
		$link = $browser->get_addressbar();
		$charset = $browser->get_charset();
		
		if (!preg_match($obj_json->getfull_reg, $response, $matches)) {
			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid fulltext regex"}';
		}

		$str_full = $matches[$obj_json->getfull_sub];
		
		//firstly call utf8 convert
		$str_full = $yos_utility->charset_process($str_full, $charset);
				
		$resource_url = new YOS_Resource_Url($str_full, $link);
		//Process link urls (href)
		$str_full = $resource_url->process_href();
		
		//Process image source urls (src)
		$str_full = $resource_url->process_src();
		
		//Find and replace here 
		if($obj_json->find_replace_full){
			$str_full = YOS_Find_Replace::find_replace($str_full, explode('|YOS_SEPARATOR|', $obj_json->find_replace_full));
		}
						
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->index = $obj_json->index;
		$obj_return->full = $str_full;
		$str_return = json_encode($obj_return);
		return $str_return;		
	}
	function step7_test()
	{
		$json = JRequest::getString('json', null, null, 2);
		if (!$json) return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid submition data"}';

		$obj_json = json_decode($json);

		if (!$obj_json->link)	return  '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid url"}';
		
		if (!isset($obj_json->index))	return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid index"}';			
		
		$arr_subpages = array();
		
		$browser = new phpWebHacks();
		$response = @$browser->get($obj_json->link);
		$str_real_url = $browser->get_addressbar();
		
		if (!$response)	return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"unable to connect"}';
		
		if ($obj_json->multiple_way == 0) {
			//find all other pages
			if (!preg_match($obj_json->pages_detect_reg, $response, $matches))	return	'{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"not found"}';
			
			$str_pagenav = $matches[$obj_json->pages_detect_sub];
			if (!preg_match_all('/<a.*?href=(.*?)(\s|\/>|>)/ism', $str_pagenav, $matches))	return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"not found #2"}';
				
			$arr_tmp = $matches[1];
			for ($i = 0; $i < count($arr_tmp); $i++){
				$arr_tmp[$i] = preg_replace('/^("|\')/', '', $arr_tmp[$i]);
				$arr_tmp[$i] = preg_replace('/("|\')$/', '', $arr_tmp[$i]);
				if ($arr_tmp[$i] == $obj_json->link) {
					continue;
				}
				$arr_subpages[] = $arr_tmp[$i];
			}
			$arr_subpages = array_unique($arr_subpages, SORT_STRING);
			
			for ($i = 0; $i < count($arr_subpages); $i++){
				$resource_url = new YOS_Resource_Url($arr_subpages[$i], $str_real_url);
				//Process link urls (href)
				$arr_subpages[$i] = $resource_url->process_single_url();
			}
		}
		else {
			//find next page, after that go to the nextpage then do find next page again and again...
			$next_page_url = $obj_json->link;
			while (1) {
				if(!$response = @$browser->get($next_page_url)){
					break;
				}
				$str_real_url = $browser->get_addressbar();
				//find all other pages
				if (!preg_match($obj_json->pages_detect_reg, $response, $matches)) {
					break;
				}
				
				$str_next_url = $matches[$obj_json->pages_detect_sub];
				
				if (!$str_next_url) {
					break;
				}
				
				$resource_url = new YOS_Resource_Url($str_next_url, $str_real_url);
				//Process link urls (href)
				$str_next_url = $resource_url->process_single_url();
				
				//to prevent loop
				if (in_array($str_next_url, $arr_subpages)) {
					break;
				}
				
				if ($str_next_url == $obj_json->link) {
					break;
				}
				
				$arr_subpages[] = $str_next_url;
				$next_page_url = $str_next_url;
			}
		}
				
		$str_subpages = implode('|YOS_SEPARATOR|', $arr_subpages);
		
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->index = $obj_json->index;
		$obj_return->subpages = $str_subpages;
		$str_return = json_encode($obj_return);
		return $str_return;	
	}
	
	function step8_test()
	{
		$yos_utility = new YOS_utility();
		
		$json = JRequest::getString('json', null, null, 2);
		if (!$json)			return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid submition data"}';

		$obj_json = json_decode($json);
		
		if (!$obj_json->link) return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid url"}';
		
		if (!isset($obj_json->index)) return	'{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"invalid index"}';
		
		$arr_subpages = array();
				
		$link						=	$obj_json->link;
		$multipage_title_reg		=	$obj_json->multipage_title_reg;
		$multipage_title_sub		=	$obj_json->multipage_title_sub;
		$multipage_reg				=	$obj_json->multipage_reg;
		$multipage_sub				=	$obj_json->multipage_sub;
		$find_replace_multipage		=	$obj_json->find_replace_multipage;
		$sub_link					=	$obj_json->sub_link;
		
		$browser = new phpWebHacks();
		$response = @$browser->get($link);		
		
		if (!$response) return '{"result":"false", "index":"'.$obj_json->index.'", "err_msg":"unable to connect"}';
		
		$response = @$browser->get($sub_link);
		
		$str_subtitle	= "";
		$str_subpage 	= "";
		if($response){
			$charset = $browser->get_charset();
			
			//find all other pages
			if ($multipage_title_reg and preg_match($multipage_title_reg, $response, $matches)) {
				$str_subtitle=$matches[$multipage_title_sub];
			}else {
				$str_subtitle="";
			}
			//firstly call utf8 convert
			$str_subtitle = $yos_utility->charset_process($str_subtitle, $charset);
			
			if (preg_match($multipage_reg, $response, $matches)){
				$str_subpage = $matches[$multipage_sub];
			}
			//firstly call utf8 convert
			$str_subpage = $yos_utility->charset_process($str_subpage, $charset);
			
			$resource_url = new YOS_Resource_Url($str_subpage, $sub_link);
			//Process link urls (href)
			$str_subpage = $resource_url->process_href();
			
			//Process image source urls (src)
			$str_subpage = $resource_url->process_src();
			
			$str_subpage = YOS_Find_Replace::find_replace($str_subpage, explode('|YOS_SEPARATOR|', $find_replace_multipage));
			
		}
		$obj_return = new stdClass();
		$obj_return->result = 'true';
		$obj_return->index = $obj_json->index;
		$obj_return->subtitle = $str_subtitle;
		$obj_return->subpage = $str_subpage;
		$str_return = json_encode($obj_return);
		return $str_return;	
	}

/*	*/
/*	*/
	
	function save()
	{
		global $mainframe;

		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		// Initialize variables
		$db		 		= &JFactory::getDBO();
		$user			= &JFactory::getUser();
		$oldtitle 		= JRequest::getString( 'oldtitle', '', 'post' );
		$post			= JRequest::get( 'post' );

		$config 	= &JComponentHelper::getParams('com_yos_news_crawler');
		$data_type 	= $config->get('data_type');
		
		$type			=	JRequest::getString('type',0);
		if(!$type)
		{
			$link=JRequest::getString('link', null, null, 2);
			
			$browser = new phpWebHacks();
			$header = @$browser->head($link);
			if(!$header || $header['Status']['Code'] != '200'){
				echo '{"type":"false"}';			
			}
			if (preg_match('/xml/i', $header['Content-Type'])) {
				$type	=	'xml';				
			}
			else {
				$type	=	'http';
			}		
		}			
		// Set the table directory
		JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR.DS.'tables');
		$row =& JTable::getInstance('YOS_News_Crawler','Table');
		$id	=	JRequest::getInt('id');
		
		if ($id) {
			$row->load($id);
		}
		if (!$row->bind( $post )) {
			JError::raiseError(500, $row->getError() );
		}
		if ($type == 'xml') {
			$options['rssUrl']=JRequest::getString('link', null, null, 2);
			$options['cache_time']=3600;

			$rssDoc =& JFactory::getXMLparser('RSS', $options);
			if ( $rssDoc == false ) {
				$msg = JText::_('Error: Feed not retrieved');
				$mainframe->redirect('index.php?option=com_yos_news_crawler&task=linkmanager', $msg);
				return;
			}

			// channel header and link
			$row->title	 	 			= $rssDoc->get_title();
			$row->url 		 			= $rssDoc->get_link();
			$row->description			= $rssDoc->get_description();
			$row->category	 			= 1;
		} else {
			$row->category				=	0;
			$row->url					=	JRequest::getString('link', null, null, 2);
			$row->title	 	  			= "";
			$row->description	 	  	= "";
		}
		
		$row->getlink_reg=JRequest::getString('getlink_reg', null, null, 2);		
		$row->find_replace_links=JRequest::getString('find_replace_links', null, null, 2);
		$row->gettitle_reg=JRequest::getString('gettitle_reg', null, null, 2);		
		$row->find_replace_titles=JRequest::getString('find_replace_titles', null, null, 2);		
		$row->getintro_reg=JRequest::getString('getintro_reg', null, null, 2);
		$row->find_replace_intro=JRequest::getString('find_replace_intro', null, null, 2);		
		$row->getfull_reg=JRequest::getString('getfull_reg', null, null, 2);		
		$row->find_replace_full=JRequest::getString('find_replace_full', null, null, 2);		
		$row->pages_detect_reg=JRequest::getString('pages_detect_reg', null, null, 2);
		$row->multipage_title_reg=JRequest::getString('multipage_title_reg', null, null, 2);
		$row->multipage_reg=JRequest::getString('multipage_reg', null, null, 2);
		$row->find_replace_multipage=JRequest::getString('find_replace_multipage', null, null, 2);
	
		if (!$row->store()) {
			JError::raiseError(500, $row->getError() );
		}
		$row->checkin();

		return $row;
	}
	// FOR EDIT LINK
/*	*/
	function getLinkEdit(){		
		if ($this->_linkEdit) {
			return $this->_linkEdit;			
		}
		global $mainframe;
		// Initialize variables
		$doTask 	= JRequest::getCmd('task',null);
		
		$db			=& JFactory::getDBO();
		$user 		=& JFactory::getUser();
		$uid		= $user->get('id');
		$option		= JRequest::getCmd( 'option' );
		$cid		= JRequest::getVar( 'cid', array(0), '', 'array' );

		JArrayHelper::toInteger($cid, array(0));
		
		$edit		=	(strtolower($doTask) == 'link.edit') || ($cid[0]);
	
		$row =& JTable::getInstance('YOS_News_Crawler','Table');
	
		// load the row from the db table
		if ($edit)
			$row->load( $cid[0] );		
	
//		 fail if checked out not by 'me'
		if ( JTable::isCheckedOut($user->get ('id'), $row->checked_out )) {
			$msg = JText::sprintf( 'DESCBEINGEDITTED', JText::_( 'The News Crawler' ), $row->link );
			$mainframe->redirect( "index.php?option=$option&task=linkmanager", $msg );
		}
		
		if ( $edit ) {
			$row->checkout( $user->get('id'));
		}
		$this->_linkEdit=$row;		
		return $this->_linkEdit;
	}	
	function getListsEdit(){
		
		$db			=	&JFactory::getDBO();
		
		$cid		= JRequest::getVar( 'cid', array(0), '', 'array' );
	
		JArrayHelper::toInteger($cid, array(0));
		$doTask 	= JRequest::getCmd('task',null);
		$edit		=	strtolower($doTask) == 'link.edit';
		$sectionid	=	0;

		$row =$this->getLinkEdit();
		// load the row from the db table
		if ($edit)
			$row->load( $cid[0] );
		
		$config 	= &JComponentHelper::getParams('com_yos_news_crawler');
		$data_type 	= $config->get('data_type');
		// build the html radio buttons for option readmore	
		$cut_content = ($row->id) ? $row->cut_content : 0;	
		$lists['cut_content'] 		= JHTML::_('select.booleanlist',  'cut_content', 'class="inputbox"', $cut_content );	
		// Select plug-in for chart
		$query = "SELECT id, name FROM #__plugins WHERE folder = 'yos_crawler' AND published = 1";
		$db->setQuery($query);		
		$plugin = $db->loadObjectList();
		$html	= '<select name="pid" id="pid">';
		$html	.='<option value="0">------------------</option>';
		$html	.= JHTML::_('select.options', $plugin, 'id', 'name', $row->pid );
		$html	.= '</select>';
		$lists['plugin_id']			= $html;
		
		// select dataway
		$dataway	=	($row->id) ? $row->dataway : 0;
		$obj		= 	new JObject();
		$obj->value	=	0;
		$obj->text	=	'MainIntro and Fulltext';
		$option[]	=	$obj;
		$obj		= 	new JObject();
		$obj->value	=	1;
		$obj->text	=	'Intro and Fulltext';
		$option[]	=	$obj;
		$obj		= 	new JObject();
		$obj->value	=	2;
		$obj->text	=	'Introtext and Cut text';
		$option[]	=	$obj;
		$obj		= 	new JObject();
		$obj->value	=	3;
		$obj->text	=	'MainIntro and Cut text';
		$option[]	=	$obj;
		$lists['dataway']		=	JHTML::_('select.genericlist', $option, 'dataway', 'class="inputbox" size="4"', 'value', 'text', $dataway);		
		return $lists;
	}
	function remove()
	{		
		// Initialize variables
		$db 	=& JFactory::getDBO();
		$cids 	= JRequest::getVar('cid', array(0), 'post', 'array');
		JArrayHelper::toInteger($cid);

		if (count( $cids ) < 1) {
			JError::raiseError(500, JText::_( 'Select a Link to delete', true ));
		}
		$arr_err=array();
		foreach ($cids as $cid)
		{
			$jobs_title=$this->getJobs($cid);
			if (count($jobs_title)) {
				$arr_err[$cid]=implode(',',$jobs_title);
				continue;
			}
			$query = 'DELETE FROM #__yos_news_crawler'
			. ' WHERE id = ( '.$cid.' )'
			;
			$db->setQuery( $query );
			if (!$db->query()) {
				JError::raiseError( 500, $db->stderr() );
				return false;
			}
			
		}
		$this->errRemove=$arr_err;
		if (count($arr_err)) {
			$this->errRemove=$arr_err;
		}
		return;
	}
	function getJobs($link_id)
	{
		$db=JFactory::getDBO();
		$query='SELECT title
				FROM `#__yos_news_crawler_jobs` 
				WHERE link_id ='.$link_id;
		$db->setQuery($query);
//		JDatabase::loadObjectList()
		return $db->loadResultArray();
	}
	function getScript(){
		
		$script=
		"
			google.load(\"language\", \"1\");
			google.setOnLoadCallback(init);
			
			function init() 
			{				
				var t_from = document.getElementById('t_from');
				var t_to = document.getElementById('t_to');
				for (l in google.language.Languages) 
				{
					var lng = l.toLowerCase();
					lng 		= lng.substring(0,1).toUpperCase()+lng.substring(1,lng.length);
					var lngCode = google.language.Languages[l];
					if (google.language.isTranslatable(lngCode)) 
					{
						t_from.options.add(new Option(lng, lngCode));
						t_to.options.add(new Option(lng, lngCode));
					}
				}
			}";
		return $script;
	}	
/*	*/
}
